<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('财务交易记录列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <!-- 账户余额显示区域 -->
            <div class="col-sm-12 mb-10" style="margin-bottom: 10px;">
                <div class="card">
                    <div class="card-body" style="padding: 15px; background-color: #f8f8f8; border: 1px solid #e7eaec; border-radius: 4px;">
                        <div class="row">
                            <div class="col-sm-4">
                                <select id="accountSelector" class="form-control" onchange="changeAccount()">
                                    <option th:each="account : ${accounts}" th:value="${account.id}" th:text="${account.name}" th:selected="${account.id == currentAccountId}"></option>
                                </select>
                            </div>
                            <div class="col-sm-4 text-center">
                                <button type="button" class="btn btn-primary" onclick="refreshBalance()">
                                    <i class="fa fa-refresh"></i> 刷新余额
                                </button>
                            </div>
                            <div class="col-sm-4 text-right">
                                <h3>当前余额: <span id="currentBalance" th:text="${currentBalance ?: '0.00'}"></span></h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>交易编号：</label>
                                <input type="text" name="transactionNo"/>
                            </li>
                            <li>
                                <label>账户ID：</label>
                                <input type="text" name="accountId"/>
                            </li>
                            <li>
                                <label>交易金额：</label>
                                <input type="text" name="amount"/>
                            </li>
                            <li>
                                <label>交易类别：</label>
                                <input type="text" name="category"/>
                            </li>
                            <li>
                                <label>交易日期：</label>
                                <input type="text" class="time-input" placeholder="请选择交易日期" name="transactionDate"/>
                            </li>
                            <li>
                                <label>交易描述：</label>
                                <input type="text" name="description"/>
                            </li>
                            <li>
                                <label>来源ID：</label>
                                <input type="text" name="sourceId"/>
                            </li>
                            <li>
                                <label>创建人ID：</label>
                                <input type="text" name="createdBy"/>
                            </li>
                            <li>
                                <label>创建时间：</label>
                                <input type="text" class="time-input" placeholder="请选择创建时间" name="createdAt"/>
                            </li>
                            <li>
                                <label>更新时间：</label>
                                <input type="text" class="time-input" placeholder="请选择更新时间" name="updatedAt"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="fin:transaction:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="fin:transaction:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="fin:transaction:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="fin:transaction:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('fin:transaction:edit')}]];
        var removeFlag = [[${@permission.hasPermi('fin:transaction:remove')}]];
        var prefix = ctx + "fin/transaction";
        var currentAccountId = [[${currentAccountId}]];

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "财务交易记录",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '',
                    visible: false
                },
                {
                    field: 'transactionNo',
                    title: '交易编号'
                },
                {
                    field: 'accountId',
                    title: '账户ID'
                },
                {
                    field: 'amount',
                    title: '交易金额'
                },
                {
                    field: 'type',
                    title: '类型'
                },
                {
                    field: 'category',
                    title: '交易类别'
                },
                {
                    field: 'transactionDate',
                    title: '交易日期'
                },
                {
                    field: 'description',
                    title: '交易描述'
                },
                {
                    field: 'sourceType',
                    title: '来源类型'
                },
                {
                    field: 'sourceId',
                    title: '来源ID'
                },
                {
                    field: 'createdBy',
                    title: '创建人ID'
                },
                {
                    field: 'createdAt',
                    title: '创建时间'
                },
                {
                    field: 'updatedAt',
                    title: '更新时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
            
            // 页面加载时自动刷新余额
            setTimeout(refreshBalance, 500);
            
            // 重写原始的操作成功回调函数
            var originalSuccessCallback = $.operate.successCallback;
            $.operate.successCallback = function(result) {
                if(originalSuccessCallback) {
                    originalSuccessCallback(result);
                }
                // 操作成功后自动刷新余额
                refreshBalance();
            };
        });
        
        // 刷新余额按钮点击事件
        function refreshBalance() {
            var accountId = $("#accountSelector").val();
            $.ajax({
                url: prefix + "/currentBalance",
                type: "get",
                data: {"accountId": accountId},
                dataType: "json",
                success: function(data) {
                    if (data.code == 0) {
                        $("#currentBalance").text(data.data);
                        // $.modal.msgSuccess("余额刷新成功");
                    } else {
                        $.modal.msgError("余额刷新失败：" + data.msg);
                    }
                },
                error: function() {
                    $.modal.msgError("余额刷新失败，请重试");
                }
            });
        }
        
        // 切换账户
        function changeAccount() {
            var accountId = $("#accountSelector").val();
            location.href = prefix + "?accountId=" + accountId;
        }
    </script>
</body>
</html>